<?php
/**
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 财付通支付响应处理
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Payment_Respond_Tenpay extends EGP_Payment_Respond_Abstract
{

    /**
     * 分析返回的结果
     *
     * @param  array  $params
     * @return array
     */
    public function parse(array $params)
    {
        if (isset($params['total_fee'])) {
            $params['total_fee'] = number_format($params['total_fee'] / 100, 2, '.', '');
        }

        return parent::parse($params);
    }

    /**
     * 获取订单号
     *
     * @return string
     */
    public function getId()
    {
        return $this->_result['sp_billno'];
    }

    /**
     * 获取支付交易号
     *
     * @return string
     */
    public function getPaymentNo()
    {
        return $this->_result['transaction_id'];
    }

    /**
     * 获取数据签名
     *
     * @return string
     */
    public function getSign()
    {
        return $this->_result['sign'];
    }

    /**
     * 获取订单总金额
     *
     * @return float
     */
    public function getAmount()
    {
        return floatval($this->_result['total_fee']);
    }

    /**
     * 获取付款时间
     *
     * @return integer
     */
    public function getTime()
    {
        return $this->_result['pay_time'];
    }

    /**
     * 获取支付状态
     *
     * @return integer
     */
    public function getStatus()
    {
        return ($this->_result['pay_result'] == 0) ?
                    EGP_Payment::STAT_PAID :
                    EGP_Payment::STAT_FAILED;
    }

    /**
     * 验证数字签名
     *
     * @throws EGP_Exception
     */
    protected function _verifySign()
    {
        extract(empty($_GET) ? $_POST : $_GET);

        $attach = isset($attach) ? $attach : '';
        $signText  = "cmdno=$cmdno&pay_result=$pay_result&date=$date"
                   . "&transaction_id=$transaction_id&sp_billno=$sp_billno"
                   . "&total_fee=$total_fee&fee_type=$fee_type&attach=$attach"
                   . "&key=" . $this->_keyCode;
        $sign = strtoupper(md5($signText));

        if ($sign != $this->getSign()) {
            throw new EGP_Exception('数字签名认证失败');
        }
    }

}